/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.figure;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Defines the view of a use case displayed as an classifier without its
 * extension points.
 * 
 * @author Gilles Cannenterre
 * @since 8 nov. 06
 */
public class UseCaseClassifierFormFigure extends BasicUseCaseClassifierFigure {

	/**
	 * 
	 */
	protected Figure containerFigure;

	/**
	 * 
	 */
	protected ExtensionPointContainerFigure extensionPointContent;

	/**
	 * The Constructor.
	 */
	public UseCaseClassifierFormFigure() {
		super();
		setOpaque(true);
		createContentPane();
	}

	/**
	 * Refresh colors.
	 * 
	 * @param graphElement the graph element
	 */
	@Override
	public void refreshColors(GraphElement graphElement) {
		super.refreshColors(graphElement);

		Font baseFont = super.getNameLabel().getFont();
		if (baseFont.getFontData() != null) {
			FontData baseFontData = baseFont.getFontData()[0];
			FontData[] labelFontData = { new FontData(baseFontData.getName(),
					baseFontData.getHeight(), SWT.BOLD) };
			Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);
			super.setFont(labelFont);
			containerLabelFigure.setFont(labelFont);
		}

		containerLabelFigure.setBackgroundColor(getBackgroundColor());
		containerLabelFigure.setFontColor(getFontColor());
		containerLabelFigure.setBorderColor(getBorderColor());
		containerLabelFigure.setForeGroundColor(getForeGroundColor());

		borderFigure.setForegroundColor(getBorderColor());
	}

	/**
	 * 
	 * 
	 * @param f 
	 */
	public void addExtensionPointContainer(ExtensionPointContainerFigure f) {
		extensionPointContent = f;
	}

	/**
	 * 
	 * 
	 * @param rect 
	 */
	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		Dimension labelFigureDimension = getNameLabel().getPreferredSize();
		Dimension borderFigureDimension = getBorderFigure().getPreferredSize();
		Dimension containerLabelFigureDimension = getContainerLabelFigure()
				.getPreferredSize();
		if (extensionPointContent != null) {
			extensionPointContent.setPreferredSize(rect.width, rect.height
					- labelFigureDimension.height
					- borderFigureDimension.height
					- containerLabelFigureDimension.height);
		}
	}

	/**
	 * 
	 */
	protected void createContentPane() {
		containerFigure = new Figure();
		ToolbarLayout layout = new ToolbarLayout(ToolbarLayout.VERTICAL);
		containerFigure.setLayoutManager(layout);
		add(containerFigure);
	}

	/**
	 * Get the inner figure accepting childs figure.
	 * 
	 * @return 
	 */
	public IFigure getContentPane() {
		return containerFigure;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.figures.IAbstractElementFigure#refreshPresentation(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Refresh presentation.
	 * 
	 * @param presentation the presentation
	 * @param selection the selection
	 */
	public void refreshPresentation(String presentation, Element selection) {
		org.eclipse.swt.graphics.Image icon =PapyrusPlugin.getIconElement(selection);

		/* if the presentation is icon or icon with text, we can set the icon */
		if ( (icon != null) 
			&& (presentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION)
				 || presentation.equals(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION)) ) {
			this.setIcon(icon);
			if (iconLabel == null) {
				createIconLabel();
			}
			iconLabel.setIcon(icon);
			iconLabel.repaint();
			this.add(iconLabel,0);
			this.repaint();		
		}
		else if (presentation.equals(ModelerHelper.DEFAULT_PRESENTATION) 
				|| presentation.equals(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION)) {
			setIcon(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(org.eclipse.uml2.uml.UseCase.class, null));
		}
		else {
			if (iconLabel != null) {
				iconLabel.setIcon(null);
				iconLabel.repaint();
				if (iconLabel.getParent() != null)	{
					this.remove(iconLabel);
				}
				this.repaint();
			}
		}
		return;
	}
	
}